## Python Script "survey_results"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=Tab Separated Values
from string import join, replace

response = context.REQUEST.RESPONSE
response.setHeader('Content-Type', 'text/tab-separated-values')

def encode(s):
	"""Replace tabs and newlines with spaces"""
	try:
		s = replace(s,'\t',4 * ' ') 
		s = replace(s,'\n',60 * ' ')
	except AttributeError: #not a string
		pass
	return str(s)

response.write("%s\n" % join(['Username','last_page_completed','end_time'] 
		              + [field.getLabel() for field in context.listQuestions(skiplabels=1)], "\t"))

for result in context.getResults():
	response.write("%s\n" % join(map(encode, result), "\t"))
